﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using System.Data;
using System.IO;

namespace LDSP.UI.Admin
{
    public partial class AddExposureDraft : BRL.BaseSysAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    this.CheckQS();
                }
                if (this.RB())
                {
                    this.InitDataDynamicBefore();
                    if (!IsPostBack)
                    {
                        this.InitDataSelect();
                    }
                    this.InitDataDynamicAfter();
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected string TitleName = "征求意见稿";
        protected string SummaryName = "概述";
        protected string Display = "";
        protected ExposureDraft m_exposureDraft = null;

        protected int LastID
        {
            get { return this.GetVSInt("LastID"); }
            set
            {
                this.SetVS("LastID", value);
                m_exposureDraft = null;
            }
        }
        protected ExposureDraft ExposureDraft
        {
            get
            {
                if (m_exposureDraft == null)
                {
                    m_exposureDraft = new ExposureDraft();
                    if (LastID != 0)
                    {
                        m_exposureDraft.GetProperties(LastID);
                    }
                    else
                    {
                        m_exposureDraft.IsActive = true;
                    }
                }
                return m_exposureDraft;
            }
        }
        protected bool IsAdd
        {
            get { return LastID == 0; }
        }

        protected void CheckQS()
        {
            LastID = this.GetInt(this.Request.QueryString["id"]);
        }
        protected bool RB()
        {
            return true;
        }
        protected void SetStatus()
        {
            this.lbl_op.Text = IsAdd ? "【添加】" : "【修改】";
            this.lbl_op.Style["color"] = IsAdd ? "red" : "blue";
            this.ClientComfirm(this.btn_ok, string.Format("您确定执行{0}操作吗？", IsAdd ? "添加" : "修改"));
        }
        protected void InitDataSelect()
        {
            this.txt_name.Text = ExposureDraft.Name;
            this.txt_comment.Text = ExposureDraft.Comment;
            this.txt_organization.Text = ExposureDraft.Organization;
            this.txt_reference_number.Text = ExposureDraft.ReferenceNumber;
            this.txt_endDT.Text = this.FormatDT(ExposureDraft.EndDT);
            this.txt_releaseDT.Text = this.FormatDT(ExposureDraft.ReleaseDT);
            this.txt_summary.Text = ExposureDraft.Summary;
            this.chk_valid.Checked = ExposureDraft.IsActive;
            this.hlk_file.Text = "";
            if (ExposureDraft.FileName != "")
            {
                this.hlk_file.Text = ExposureDraft.FileName;
            }

            SetStatus();
        }
        protected void InitDataDynamicBefore()
        {
        }
        protected void InitDataDynamicAfter()
        {
            this.JSSetDateTimeReadOnly(this.txt_endDT.ClientID);
            this.JSSetDateTimeReadOnly(this.txt_releaseDT.ClientID);
        }

        protected void ValidateDataBefore()
        {
        }
        protected void ValidateDataAfter()
        {
            if (ExposureDraft.Name == "")
            {
                throw new BYFW.BOL.CustomException("名称不能为空！");
            }
        }
        protected void SetData()
        {
            this.ValidateDataBefore();
            ExposureDraft.Name = this.txt_name.Text.Trim();
            ExposureDraft.Comment = this.txt_comment.Text;
            ExposureDraft.ReleaseDT = DateTime.Parse(this.txt_releaseDT.Text.Trim());
            ExposureDraft.EndDT = DateTime.Parse(this.txt_endDT.Text.Trim());
            ExposureDraft.Organization = this.txt_organization.Text.Trim();
            ExposureDraft.ReferenceNumber = this.txt_reference_number.Text.Trim();
            ExposureDraft.Summary = this.txt_summary.Text;
            ExposureDraft.IsActive = this.chk_valid.Checked;
            if (this.fup_file.HasFile)
            {
                ExposureDraft.CreateNewFile(this.fup_file.FileName);
                this.fup_file.SaveAs(ExposureDraft.GetDownloadFileFullName());
                ExposureDraft.UploadDT = DateTime.Parse(DateTime.Now.ToShortDateString());
            }
            else
            {
                ExposureDraft.UploadDT = new DateTime(1753, 1, 1);
            }
            this.ValidateDataAfter();
        }
        protected void Add()
        {
            this.SetData();
            ExposureDraft.Add();
            this.LastID = 0;
            this.JSAlert("添加成功！");
        }
        protected void Update()
        {
            string tmpName = ExposureDraft.Name;
            DateTime tmpEndDT = ExposureDraft.EndDT;

            this.SetData();

            if (tmpName != ExposureDraft.Name)
            {
                new DownloadLog().UpdateByDocName(ExposureDraft.ID, DocType.征求意见稿, ExposureDraft.Name);
            }

            ExposureDraft.Update();
            this.JSAlert("更新成功！");
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                this.LastID = 0;
                this.InitDataSelect();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsAdd)
                {
                    this.Add();
                }
                else
                {
                    this.Update();
                }
                this.InitDataSelect();

            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
    }

}